package cn.xiyue.boxuegu;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import cn.xiyue.boxuegu.base.BaseActivity;
import cn.xiyue.boxuegu.utils.MD5Utils;

public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private TextView tv_back;
    private TextView tv_main_title;
    private ImageView iv_head;
    private EditText et_user_name;
    private EditText et_psw;
    private Button btn_login;
    private TextView tv_register;
    private TextView tv_find_psw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    private void initView() {
        tv_back = (TextView) findViewById(R.id.tv_back);
        tv_main_title = (TextView) findViewById(R.id.tv_main_title);
        iv_head = (ImageView) findViewById(R.id.iv_head);
        et_user_name = (EditText) findViewById(R.id.et_user_name);
        et_psw = (EditText) findViewById(R.id.et_psw);
        btn_login = (Button) findViewById(R.id.btn_login);
        tv_register = (TextView) findViewById(R.id.tv_register);
        tv_find_psw = (TextView) findViewById(R.id.tv_find_psw);

        tv_back.setOnClickListener(v -> finish());
        tv_main_title.setText("登录");
        btn_login.setOnClickListener(this);
        tv_register.setOnClickListener(this);
        tv_find_psw.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                submit();
                break;
            case R.id.tv_register:
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivityForResult(intent,1);
                break;
            case R.id.tv_find_psw:
                break;
        }
    }

    private void submit() {
        // validate
        String name = et_user_name.getText().toString().trim();
        String psw = et_psw.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            showMsg("用户名不能为空");
        } else if (TextUtils.isEmpty(psw)) {
            showMsg("密码不能为空");
        }else {
            String md5 = MD5Utils.md5(psw);
            if (md5.equals(getSP().getString(psw,""))){
                showMsg("登录成功");
                getSPEditor().putBoolean("isLogin",true).putString("loginUserName",name).apply();
                Intent intent = new Intent();
                intent.putExtra("isLogin",true);
                setResult(RESULT_OK,intent);
                finish();
            }else {
                showMsg("输入的用户名和密码不一致");
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data!=null){
            String userName = data.getStringExtra("userName");
            if (!TextUtils.isEmpty(userName)){
                et_user_name.setText(userName);
                et_user_name.setSelection(userName.length());
            }
        }
    }
}